//
//  MyCommentViewController.m
//  yunyan
//
//  Created by haichuang on 2018/3/14.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "MyCommentViewController.h"
#import "MyCommentCell.h"
#import "NewsDetailsViewController.h"
#import "ActiveDetailViewController.h"
@interface MyCommentViewController ()<UITableViewDelegate, UITableViewDataSource>{
    
    NSArray * _dataArray;
}
@property (nonatomic, strong) UITableView * commentTab;
@end

@implementation MyCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

   [self customNaviItemTitle:@"我的评论" isLeft:YES];
    self.commentTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:_commentTab];
    
    WeakSelf;
    [_commentTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo (weakSelf.view);
       
    }];
    
    self.commentTab.delegate = self;
    self.commentTab.dataSource = self;
    self.commentTab.estimatedRowHeight = 107.f;
    self.commentTab.rowHeight = UITableViewAutomaticDimension;
    [self.commentTab registerNib:[UINib nibWithNibName:@"MyCommentCell" bundle:nil] forCellReuseIdentifier:@"MyCommentCell"];
    
    [self getCommentListRequest];
}


#pragma mark - tableview delegete;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataArray.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.f;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
      NSDictionary * dic = _dataArray[indexPath.row];
       MyCommentCell * cell=[tableView dequeueReusableCellWithIdentifier:@"MyCommentCell"forIndexPath:indexPath ];
    cell.timeLabel.text = dic[@"create_time"];
    cell.titleNameLabel.text = dic[@"content"];
    cell.detailsLabel.text = dic[@"title"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * dic = _dataArray[indexPath.row];
    
    if ([dic[@"type"] intValue]==1) {
        
        NSLog(@"新闻");
      
        NewsDetailsViewController * homeDetiel = [[NewsDetailsViewController alloc]init];
        homeDetiel.news_idStr = [NSString stringWithFormat:@"%ld",[dic[@"news_id"] integerValue]];
//        homeDetiel.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:homeDetiel animated:YES];
    }else {
        NSLog(@"随手拍");
        
        [self getActDetailsRequestForid:[NSString stringWithFormat:@"%ld",[dic[@"news_id"] integerValue]]];
    }
    
    
}


-(void)getActDetailsRequestForid:(NSString *)newsId{
    
    NSDictionary * dic = @{@"news_id":newsId,
                           @"user_id":USER_DEFAULTGet(@"user_id")
                           };
    
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(activity_details_api) andparameters:dic Success:^(id responseObject) {
        
        NSDictionary *  dataDic = responseObject;
        NSLog(@"responseObject = %@",responseObject);
        
        
        
        NSLog(@"responseObject = %@",responseObject);
        
        USER_DEFAULTSet(dataDic[@"pic_url"], @"actDetailsIamgeArray");
        USER_DEFAULTSet(dataDic[@"is_fabulous"], @"act_is_fabulous");
        
        
        if ([dataDic[@"pic_url"] isKindOfClass:[NSArray class]]) {
            NSArray * array = dataDic[@"pic_url"];
            if (array.count==0) {
                
                USER_DEFAULTSet(@"http://a1.qpic.cn/psb?/V147v55d35m6eX/xXdZ2De4s1iE5gYJaikuWyqltDsmFf*LHHZ1ANFPtso!/m/dDABAAAAAAAA&bo=eAB4AAAAAAADByI!&rf=photolist", @"shareImage");
            }else {
                
                USER_DEFAULTSet(dataDic[@"pic_url"][0], @"shareImage");
            }
        }
        NSString * newsId= [NSString stringWithFormat:@"%ld",[dataDic[@"id"] integerValue]];
        USER_DEFAULTSet(newsId,@"shareNewsId");
        USER_DEFAULTSet(@"2",@"shareType");
        USER_DEFAULTSet(dataDic[@"title"], @"shareTitle");
        USER_DEFAULTSet(dataDic[@"content"], @"shareContent");
        USER_DEFAULTSet(dataDic[@"share_url"], @"shareUrl");
        
        
        ActiveDetailViewController * activeDetail = [[ActiveDetailViewController alloc]init];
        activeDetail.actIdStr = newsId;
        activeDetail.dataDic = dataDic;
        activeDetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:activeDetail animated:YES];
        
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
    
}

-(void)getCommentListRequest{
    
    NSDictionary * dic = @{
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"mac_code":[SHJudgeTools getDeviceId]
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_myCommentList_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject isKindOfClass:[NSArray class]]) {
            
            _dataArray =  responseObject;
            
            [_commentTab reloadData];
        }else{
            
            SHuiMBhudTextTimeStr(responseObject[@"msg"]);
        }
        
        [_commentTab.mj_header endRefreshing];
      
        NSLog(@"responseObject = %@",responseObject);
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
        [_commentTab.mj_header endRefreshing];
        [_commentTab.mj_footer endRefreshing];
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else {
            SHuiMBhudTextNetError;
        }
    }];
}

@end
